/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

XiaoNuo采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改XiaoNuo源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/xiaonuo-vue
6.若您的项目无法满足以上几点，可申请商业授权，获取XiaoNuo商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package com.cn.henry.sys.modular.interpersonal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.henry.core.consts.CommonConstant;
import com.cn.henry.core.enums.CommonStatusEnum;
import com.cn.henry.core.exception.ServiceException;
import com.cn.henry.core.factory.PageFactory;
import com.cn.henry.core.pojo.page.PageResult;
import com.cn.henry.sys.modular.interpersonal.entity.Interpersonal;
import com.cn.henry.sys.modular.interpersonal.enums.InterpersonalExceptionEnum;
import com.cn.henry.sys.modular.interpersonal.mapper.InterpersonalMapper;
import com.cn.henry.sys.modular.interpersonal.param.InterpersonalParam;
import com.cn.henry.sys.modular.interpersonal.service.InterpersonalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 人情来往表service接口实现类
 *
 * @author henry
 * @date 2021-01-27 17:16:36
 */
@Service
public class InterpersonalServiceImpl extends ServiceImpl<InterpersonalMapper, Interpersonal> implements InterpersonalService {

    @Override
    public PageResult<Interpersonal> page(InterpersonalParam interpersonalParam) {
        QueryWrapper<Interpersonal> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(interpersonalParam)) {

            // 根据自增ID 查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getId())) {
                queryWrapper.lambda().eq(Interpersonal::getId, interpersonalParam.getId());
            }

            // 根据用户id 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getUserId())) {
                queryWrapper.lambda().like(Interpersonal::getUserId, interpersonalParam.getUserId());
            }

            // 根据家庭id 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getFamilyId())) {
                queryWrapper.lambda().like(Interpersonal::getFamilyId, interpersonalParam.getFamilyId());
            }

            // 根据对方姓名 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getName())) {
                queryWrapper.lambda().like(Interpersonal::getName, interpersonalParam.getName());
            }

            // 根据金额 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getMoney())) {
                queryWrapper.lambda().like(Interpersonal::getMoney, interpersonalParam.getMoney());
            }

            // 根据本人亲到,是Y,否N 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getIsSelf())) {
                queryWrapper.lambda().like(Interpersonal::getIsSelf, interpersonalParam.getIsSelf());
            }

            // 根据送礼日期 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftDate())) {
                queryWrapper.lambda().like(Interpersonal::getGiftDate, interpersonalParam.getGiftDate());
            }

            // 根据送礼原因 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftReason())) {
                queryWrapper.lambda().like(Interpersonal::getGiftReason, interpersonalParam.getGiftReason());
            }

            // 根据送礼类型,送礼1,收礼2 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftType())) {
                queryWrapper.lambda().like(Interpersonal::getGiftType, interpersonalParam.getGiftType());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getRemark())) {
                queryWrapper.lambda().like(Interpersonal::getRemark, interpersonalParam.getRemark());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getState())) {
                queryWrapper.lambda().like(Interpersonal::getState, interpersonalParam.getState());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getExt1())) {
                queryWrapper.lambda().like(Interpersonal::getExt1, interpersonalParam.getExt1());
            }

        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<Interpersonal> list(InterpersonalParam interpersonalParam) {
        LambdaQueryWrapper<Interpersonal> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotNull(interpersonalParam)) {

            // 根据自增ID 查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getId())) {
                queryWrapper.eq(Interpersonal::getId, interpersonalParam.getId());
            }

            // 根据用户id 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getUserId())) {
                queryWrapper.like(Interpersonal::getUserId, interpersonalParam.getUserId());
            }

            // 根据家庭id 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getFamilyId())) {
                queryWrapper.like(Interpersonal::getFamilyId, interpersonalParam.getFamilyId());
            }

            // 根据对方姓名 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getName())) {
                queryWrapper.like(Interpersonal::getName, interpersonalParam.getName());
            }

            // 根据金额 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getMoney())) {
                queryWrapper.like(Interpersonal::getMoney, interpersonalParam.getMoney());
            }

            // 根据本人亲到,是Y,否N 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getIsSelf())) {
                queryWrapper.like(Interpersonal::getIsSelf, interpersonalParam.getIsSelf());
            }

            // 根据送礼日期 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftDate())) {
                queryWrapper.like(Interpersonal::getGiftDate, interpersonalParam.getGiftDate());
            }

            // 根据送礼原因 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftReason())) {
                queryWrapper.like(Interpersonal::getGiftReason, interpersonalParam.getGiftReason());
            }

            // 根据送礼类型,送礼1,收礼2 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getGiftType())) {
                queryWrapper.like(Interpersonal::getGiftType, interpersonalParam.getGiftType());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getRemark())) {
                queryWrapper.like(Interpersonal::getRemark, interpersonalParam.getRemark());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getState())) {
                queryWrapper.like(Interpersonal::getState, interpersonalParam.getState());
            }

            // 根据 模糊查询
            if (ObjectUtil.isNotEmpty(interpersonalParam.getExt1())) {
                queryWrapper.like(Interpersonal::getExt1, interpersonalParam.getExt1());
            }

        }
        return this.list(queryWrapper);
    }

    @Override
    public void add(InterpersonalParam interpersonalParam) {
        Interpersonal interpersonal = new Interpersonal();
        BeanUtil.copyProperties(interpersonalParam, interpersonal);
        this.save(interpersonal);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(InterpersonalParam interpersonalParam) {
            this.removeById(interpersonalParam.getId());
                                                    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(InterpersonalParam interpersonalParam) {
        Interpersonal interpersonal = this.queryInterpersonal(interpersonalParam);
        BeanUtil.copyProperties(interpersonalParam, interpersonal);
        this.updateById(interpersonal);
    }

    @Override
    public Interpersonal detail(InterpersonalParam interpersonalParam) {
        return this.queryInterpersonal(interpersonalParam);
    }

    /**
     * 获取人情来往表
     *
     * @author henry
     * @date 2021-01-27 17:16:36
     */
    private Interpersonal queryInterpersonal(InterpersonalParam interpersonalParam) {
        Interpersonal interpersonal = this.getById(interpersonalParam.getId());
        if (ObjectUtil.isNull(interpersonal)) {
            throw new ServiceException(InterpersonalExceptionEnum.NOT_EXIST);
        }
        return interpersonal;
    }
}
